package com.example.ymm.modules1.service.impl.hl.ex;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.ExPublishQuestion;
import com.example.ymm.modules1.mapper.ex.ExPublishQuestionMapper;
import com.example.ymm.modules1.query.hl.ex.ExPublishQuestionQueryCriteria;
import com.example.ymm.modules1.service.hl.ex.ExPublishQuestionService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
@DS("slave2")
public class ExPublishQuestionServiceImpl extends BaseServiceImpl<ExPublishQuestionMapper, ExPublishQuestion> implements ExPublishQuestionService {

    @Override
    public TableList<ExPublishQuestion> list(ExPublishQuestionQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExPublishQuestion> page = new Page<ExPublishQuestion>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExPublishQuestion.class, criteria);
        predicate.orderByAsc("sort");
        baseMapper.selectPage(page, predicate);
        TableList<ExPublishQuestion> tableList=new TableList(page);
        return tableList;
    }

    public List<ExPublishQuestion> queryAll(ExPublishQuestionQueryCriteria criteria) {
//        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExPublishQuestion.class, criteria);
//        predicate.orderByAsc("sort");
        List<ExPublishQuestion> result =baseMapper.queryList(criteria);
        for (ExPublishQuestion exPublishQuestion : result) {
            List<String> imgArr= new ArrayList<>();
            if(UtilValidate.isNotEmpty(exPublishQuestion.getImg())){
                String[] split = exPublishQuestion.getImg().split(",");
                imgArr=Arrays.asList(split);
            }
            exPublishQuestion.setImgArr(imgArr);
        }
        return result;
    }

    @Override
    public ExPublishQuestion queryOne(ExPublishQuestionQueryCriteria criteria) {
        ExPublishQuestion publishQuestion = baseMapper.queryOne(criteria);
        return publishQuestion;
    }
}
